package org.jmc.refmanager.web.wicket.model;

import org.apache.wicket.Component;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.lang.PropertyResolver;

public class EnumPropertyModel<T extends Enum<T>> extends PropertyModel<String> {
	private static final long serialVersionUID = 1L;

	private Component component;
	private Class<T> enumType;
	
	public EnumPropertyModel(Object modelObject, String expression, Component resourceProvider, Class<T> t) {
		super(modelObject, expression);
		this.component = resourceProvider;
		this.enumType = t;
	}
 
	@Override
	public String getObject() {
		final String expression = propertyExpression();
		final Object target = getTarget();
		if (target != null) {
			@SuppressWarnings("unchecked")
			T enumValue = (T) PropertyResolver.getValue(expression, target);
			String key = EnumMessageKeyProvider.getMessageKey(enumValue, enumType);
			return component.getString(key);
		}
		return null;
	}

}
